﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using ECare.Client.templates;
using ECare.BUS.Data;
using ECare.BUS;
//
namespace ECare.Client.frms
{
    public partial class frmSupplyInfo : frmInfo
    {
        public frmSupplyInfo()
        {
            InitializeComponent();

            this.Load += frmSupplyInfo_Load;
        }

        void frmSupplyInfo_Load(object sender, EventArgs e)
        {
            if (this.entity != null)
            {
                var resource=this.entity as ResourceSupplyInfo;
                this.txtAddress.Text = resource.resource_supply_address;
                this.txtEmail.Text = resource.resource_supply_email;
                this.txtName.Text = resource.resource_supply_name;
                this.txtPhone.Text = resource.resource_supply_phone;
                this.txtUN.Text = resource.resource_supply_un;

                this.txtCode.Text = resource.resource_supply_code;
            }
        
        }

        public event EntitySavedHandler Saved;

        private void btnSave_Click(object sender, EventArgs e)
        {
            if (!ValidateTextControl(this.txtName))
                return;
            if (!ValidateTextControl(this.txtCode))
                return;

            if (!ValidateTextControl(this.txtUN))
                return;
            if (!ValidateTextControl(this.txtPhone))
                return;
            if (!ValidateTextControl(this.txtAddress))
                return;

            if (this.entity == null)
            {
                var newsupply = new ResourceSupplyInfo();
                newsupply.resource_supply_name = this.txtName.Text;
                newsupply.resource_supply_un = txtUN.Text;
                newsupply.resource_supply_phone = txtPhone.Text;
                newsupply.resource_supply_email = txtEmail.Text;
                newsupply.resource_supply_address = txtAddress.Text;
                newsupply.resource_supply_code = this.txtCode.Text;

                var supplyId = ResourceSupplyHelper.CreateResourceSupply(newsupply);
                newsupply.resource_supply_id = supplyId;
                this.entity = newsupply;
            }
            else
            {
                var newsupply = this.entity as ResourceSupplyInfo;
                newsupply.resource_supply_name = this.txtName.Text;
                newsupply.resource_supply_un = txtUN.Text;
                newsupply.resource_supply_phone = txtPhone.Text;
                newsupply.resource_supply_email = txtEmail.Text;
                newsupply.resource_supply_address = txtAddress.Text;
                newsupply.resource_supply_code = this.txtCode.Text;
                ResourceSupplyHelper.UpdateResourceInfo(newsupply);
            }
            if (this.Saved != null)
            {
                this.Saved(this.entity);
                this.Close();
            }

        }

        private void txtName_TextChanged(object sender, EventArgs e)
        {

        }

        private void eButton1_Click_1(object sender, EventArgs e)
        {
            
            //var worker = new SupplyInfoReportBuilder { CurrentResourceInfo = this.entity as ResoucesInfo };
            //worker.RunWorkerCompleted += worker_RunWorkerCompleted;
            //worker.RunWorkerAsync();
            //frmloading = new frmLoading();
            //frmloading.ShowDialog();
             
        }
        void worker_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
        {
            //frmloading.Close();

           // var frmprint = new frmPrint(); frmprint.file = e.Result as string;
            //frmprint.ShowDialog();
        }

        private void txtName_Leave(object sender, EventArgs e)
        {
            bool flag;

            if (this.entity == null)
            {
                var newresourcesupply = new ResourceSupplyInfo
                {
                    resource_supply_name = this.txtName.Text,
                    resource_supply_code = this.txtCode.Text,
                };
                flag = ResourceSupplyHelper.CheckResourceSupplyName(newresourcesupply);
            }
            else
            {
                var editresourcesupply = this.entity as ResourceSupplyInfo;
                editresourcesupply.resource_supply_name = this.txtName.Text;
                editresourcesupply.resource_supply_code = this.txtCode.Text;
                flag = ResourceSupplyHelper.CheckResourceSupplyName(editresourcesupply);
            }
            if (!flag)
            {
                this.errorProvider1.SetError(this.txtName, "The resource supply name or resource code has existed.");
            }
            else
            {
                this.errorProvider1.Clear();
            }
        }

        private void eButton1_Click(object sender, EventArgs e)
        {

        }

    }
}
